package com.xysq.lemon.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.flyco.dialog.listener.OnBtnClickL;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xysq.activity.BaseActivity;
import com.xysq.activity.CustomerPayActivity;
import com.xysq.activity.MyPublishedActivity;
import com.xysq.activity.MyTicketsActivity;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.WeiChatDialog;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, PropertiesUtil.getAppWechatKey());
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.d("test", "req");
    }

    @Override
    public void onResp(BaseResp resp) {
        final boolean result = UserInfoKeeper.readStatus(this);
        if (resp.errCode == 0) {
            if (result) {
                CustomerPayActivity customerPayActivity = new CustomerPayActivity();
                customerPayActivity.confirmOreder(UserInfoKeeper.readOrderId(this), UserInfoKeeper.readKcuoponModelId(this), UserInfoKeeper.readWord(this), UserInfoKeeper.readPhoneNumber(this),false);
            }
            final WeiChatDialog dialog = new WeiChatDialog(WXPayEntryActivity.this);
            String message;
            if (result) {//c端
                dialog.content(getResources().getString(R.string.dialog_success_buy_C));
//                builder.setTitle(R.string.dialog_success_buy_C);
                message = getResources().getString(R.string.dialog_my_reward);
            } else {
                dialog.content(getResources().getString(R.string.dialog_success_buy_B));
//                builder.setTitle(R.string.dialog_success_buy_B);g
                message = getResources().getString(R.string.dialog_my_tickets);
            }
            dialog.isTitleShow(false)
                    .btnText(message)
                    .show();
            dialog.setCancelable(false);
            dialog.setOnBtnClickL(new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    if (result) {//C端
                        Intent intent = new Intent(WXPayEntryActivity.this, MyPublishedActivity.class);
                        intent.putExtra(MyPublishedActivity.INDEX, 3);//已打赏
                        startActivity(intent);
                    } else {//B端
                        Intent intent = new Intent(WXPayEntryActivity.this, MyTicketsActivity.class);
                        startActivity(intent);
                    }
                    dialog.dismiss();
                    finish();
                }
            });
//            AlertDialog.Builder builder = new AlertDialog.Builder(WXPayEntryActivity.this);
//            String message;
//            if (result) {//c端
//                builder.setTitle(R.string.dialog_success_buy_C);
//                message = getResources().getString(R.string.dialog_my_reward);
//            } else {
//                builder.setTitle(R.string.dialog_success_buy_B);
//                message = getResources().getString(R.string.dialog_my_tickets);
//            }
//            builder.setCancelable(false);
//            builder.setPositiveButton(message,
//                    new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            if (result) {//C端
//                                Intent intent = new Intent(WXPayEntryActivity.this, MyPublishedActivity.class);
//                                intent.putExtra(MyPublishedActivity.INDEX,3);//已打赏
//                                startActivity(intent);
//                            } else {//B端
//                                Intent intent = new Intent(WXPayEntryActivity.this, MyTicketsActivity.class);
//                                startActivity(intent);
//                            }
//                            dialog.dismiss();
//                            finish();
//                        }
//                    });
//            builder.show();
        }else {
            finish();
        }
    }
}